<template>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="account-form">
        <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username" disabled />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
            <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="个人简介" prop="bio">
            <el-input v-model="form.bio" type="textarea" :rows="4" placeholder="请输入个人简介" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" :loading="loading" @click="submit">保存</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
export default {
    props: {
        user: {
            type: Object,
            required: true
        }
    },
    data() {
        return {
            loading: false,
            form: {
                username: this.user.userId || '',
                nickname: this.user.name || '',
                phone: '',
                email: this.user.email || '',
                bio: ''
            },
            rules: {
                nickname: [
                    { required: true, message: '请输入昵称', trigger: 'blur' },
                    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
                ],
                phone: [
                    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
                ],
                email: [
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        submit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.loading = true;
                    // 实际项目中这里应该调用修改用户信息的API
                    setTimeout(() => {
                        this.$message.success('个人信息修改成功');
                        this.loading = false;
                        // 成功后更新用户信息
                        this.$store.dispatch('user/setName', this.form.nickname);
                    }, 600);
                } else {
                    return false;
                }
            });
        }
    }
}
</script>

<style lang="scss" scoped>
.account-form {
    max-width: 600px;
    margin: 0 auto;
    padding: 20px 0;

    .el-form-item {
        margin-bottom: 22px;
    }
}
</style>